import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../services/native_wechat_share_service.dart';
import '../utils/toast_util.dart';
import 'dart:developer' as developer;

/// 🔥 微信分享选择对话框
/// 
/// 类似MyApplication2项目的分享UI设计
/// 提供微信好友和朋友圈两种分享选项
class WeChatShareDialog extends StatelessWidget {
  final String title;
  final String description;
  final String webUrl;
  final String? thumbUrl;

  const WeChatShareDialog({
    Key? key,
    required this.title,
    required this.description,
    required this.webUrl,
    this.thumbUrl,
  }) : super(key: key);

  /// 显示分享对话框的静态方法
  static void show({
    required String title,
    required String description,
    required String webUrl,
    String? thumbUrl,
  }) {
    Get.bottomSheet(
      WeChatShareDialog(
        title: title,
        description: description,
        webUrl: webUrl,
        thumbUrl: thumbUrl,
      ),
      isScrollControlled: true,
      backgroundColor: Colors.transparent,
      enableDrag: true,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(20),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.1),
            blurRadius: 10,
            offset: const Offset(0, 5),
          ),
        ],
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          // 拖拽指示器
          _buildDragIndicator(),
          
          // 标题
          _buildTitle(),
          
          // 分享选项
          _buildShareOptions(context),
          
          // 取消按钮
          _buildCancelButton(),
        ],
      ),
    );
  }

  /// 构建拖拽指示器
  Widget _buildDragIndicator() {
    return Container(
      margin: const EdgeInsets.only(top: 12),
      width: 40,
      height: 4,
      decoration: BoxDecoration(
        color: Colors.grey[300],
        borderRadius: BorderRadius.circular(2),
      ),
    );
  }

  /// 构建标题
  Widget _buildTitle() {
    return Padding(
      padding: const EdgeInsets.fromLTRB(20, 20, 20, 10),
      child: Text(
        '分享到',
        style: TextStyle(
          fontSize: 18,
          fontWeight: FontWeight.w600,
          color: Colors.grey[800],
        ),
      ),
    );
  }

  /// 构建分享选项
  Widget _buildShareOptions(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 20),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          // 微信好友
          _buildShareOption(
            icon: Icons.wechat,
            iconColor: Colors.green,
            label: '微信好友',
            onTap: () => _shareToFriend(context),
          ),
          
          // 微信朋友圈
          _buildShareOption(
            icon: Icons.public,
            iconColor: Colors.blue,
            label: '微信朋友圈',
            onTap: () => _shareToTimeline(context),
          ),
        ],
      ),
    );
  }

  /// 构建单个分享选项
  Widget _buildShareOption({
    required IconData icon,
    required Color iconColor,
    required String label,
    required VoidCallback onTap,
  }) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 15),
        decoration: BoxDecoration(
          color: iconColor.withOpacity(0.1),
          borderRadius: BorderRadius.circular(15),
          border: Border.all(
            color: iconColor.withOpacity(0.3),
            width: 1,
          ),
        ),
        child: Column(
          children: [
            // 图标
            Container(
              width: 50,
              height: 50,
              decoration: BoxDecoration(
                color: iconColor.withOpacity(0.2),
                borderRadius: BorderRadius.circular(25),
              ),
              child: Icon(
                icon,
                size: 28,
                color: iconColor,
              ),
            ),
            
            const SizedBox(height: 8),
            
            // 标签
            Text(
              label,
              style: TextStyle(
                fontSize: 14,
                fontWeight: FontWeight.w500,
                color: Colors.grey[700],
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// 构建取消按钮
  Widget _buildCancelButton() {
    return Padding(
      padding: const EdgeInsets.fromLTRB(20, 10, 20, 20),
      child: GestureDetector(
        onTap: () => Get.back(),
        child: Container(
          width: double.infinity,
          padding: const EdgeInsets.symmetric(vertical: 15),
          decoration: BoxDecoration(
            color: Colors.grey[100],
            borderRadius: BorderRadius.circular(12),
            border: Border.all(
              color: Colors.grey[300]!,
              width: 1,
            ),
          ),
          child: Text(
            '取消',
            textAlign: TextAlign.center,
            style: TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.w500,
              color: Colors.grey[600],
            ),
          ),
        ),
      ),
    );
  }

  /// 分享到微信好友
  void _shareToFriend(BuildContext context) async {
    try {
      Get.back(); // 关闭对话框
      
      developer.log('🤝 分享到微信好友');
      
      // 获取原生微信分享服务
      final weChatShareService = Get.find<NativeWeChatShareService>();
      
      // 执行分享
      final success = await weChatShareService.shareToFriend(
        title: title,
        description: description,
        webUrl: webUrl,
        thumbUrl: thumbUrl,
      );
      
      if (!success) {
        developer.log('❌ 分享到微信好友失败');
      }
      
    } catch (e) {
      developer.log('💥 分享到微信好友异常: $e');
      ToastUtil.error('分享失败: $e');
    }
  }

  /// 分享到微信朋友圈
  void _shareToTimeline(BuildContext context) async {
    try {
      Get.back(); // 关闭对话框
      
      developer.log('🌍 分享到微信朋友圈');
      
      // 获取原生微信分享服务
      final weChatShareService = Get.find<NativeWeChatShareService>();
      
      // 执行分享
      final success = await weChatShareService.shareToTimeline(
        title: title,
        description: description,
        webUrl: webUrl,
        thumbUrl: thumbUrl,
      );
      
      if (!success) {
        developer.log('❌ 分享到微信朋友圈失败');
      }
      
    } catch (e) {
      developer.log('💥 分享到微信朋友圈异常: $e');
      ToastUtil.error('分享失败: $e');
    }
  }
}
